package org.eenie.learn.seedquartz.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DealStrSub {

    /**
     * 正则表达式匹配两个指定字符串中间的内容
     * @param soap
     * @return
     */
    public static List<String> getSubUtil(String soap,String rgex){
        List<String> list = new ArrayList<String>();
        Pattern pattern = Pattern.compile(rgex);// 匹配的模式
        Matcher m = pattern.matcher(soap);
        while (m.find()) {
            int i = 1;
            list.add(m.group(i));
            i++;
        }
        return list;
    }

    /**
     * 返回单个字符串，若匹配到多个的话就返回第一个，方法与getSubUtil一样
     * @param soap
     * @param rgex
     * @return
     */
    public static String getSubUtilSimple(String soap,String rgex){
        Pattern pattern = Pattern.compile(rgex);// 匹配的模式
        Matcher m = pattern.matcher(soap);
        while(m.find()){
            return m.group(1);
        }
        return "";
    }

//    /**
//     * 测试
//     * @param args
//     */
//    public static void main(String[] args) {
//        String str = "    <script>\n" +
//                "        var MaxQuantity=3;\n" +
//                "        var shipstr='沈家门-嵊山/枸杞-2019-09-29-07:45';\n" +
//                "        var shippricelst=[{\"seatType\":\"中舱\",\"seatTypeCode\":\"0113\",\"ticketType\":\"儿童票\",\"ticketTypeCode\":\"02\",\"price\":\"91.00\"},{\"seatType\":\"卧铺\",\"seatTypeCode\":\"0114\",\"ticketType\":\"儿童票\",\"ticketTypeCode\":\"02\",\"price\":\"101.00\"},{\"seatType\":\"上舱\",\"seatTypeCode\":\"0112\",\"ticketType\":\"儿童票\",\"ticketTypeCode\":\"02\",\"price\":\"128.00\"},{\"seatType\":\"中舱\",\"seatTypeCode\":\"0113\",\"ticketType\":\"成人票\",\"ticketTypeCode\":\"01\",\"price\":\"180.00\"},{\"seatType\":\"卧铺\",\"seatTypeCode\":\"0114\",\"ticketType\":\"成人票\",\"ticketTypeCode\":\"01\",\"price\":\"200.00\"},{\"seatType\":\"上舱\",\"seatTypeCode\":\"0112\",\"ticketType\":\"成人票\",\"ticketTypeCode\":\"01\",\"price\":\"255.00\"}];\n" +
//                "        var isLogin = 'True'.toLowerCase() === 'true';\n" +
//                "        //未成年人是否能购保险\n" +
//                "        var minorBuyIns = 'false' === 'true';\n" +
//                "    </script>";
//        String rgex = "shippricelst=(.*?);";
//        System.out.println(getSubUtil(str,rgex));
//        System.out.println(getSubUtilSimple(str, rgex));
//    }
}
